//
//  TermLabel.m
//  eGifr
//
//  Created by Phien Tram on 8/6/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "TermLabel.h"

#define TERM_LABEL_TRANSLATION_AMOUNT_TO_FLICK     5.0f

@implementation TermLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _state = TERM_TOUCH_EVENT_STATE_RESUMED;
        
        
        
        [self addTapAndPanGestureRecognizer];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)addTapAndPanGestureRecognizer
{
    UIGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self addGestureRecognizer:gestureRecognizer];
    
    gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self addGestureRecognizer:gestureRecognizer];
}

- (void)removeAllGestureRecognizers
{
    for (UIGestureRecognizer *gestureRecognizer in self.gestureRecognizers) {
        [self removeGestureRecognizer:gestureRecognizer];
    }
}

- (void)handleTap:(UIGestureRecognizer *)gestureRecognizer
{
    NSLog(@"handleTap:%d", self.tag);
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        _state = TERM_TOUCH_EVENT_STATE_PAUSED;
    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        _state = TERM_TOUCH_EVENT_STATE_FINISH_FLICK_UP;
    }
}

- (void)handlePan:(UIGestureRecognizer *)gestureRecognizer
{
//    NSLog(@"handlePan:%d", self.tag);
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        _state = TERM_TOUCH_EVENT_STATE_PAUSED;
    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        if (_state == TERM_TOUCH_EVENT_STATE_FLICK_UP) {
            _state = TERM_TOUCH_EVENT_STATE_FINISH_FLICK_UP;
            [self removeGestureRecognizer:gestureRecognizer];
        } else if (_state == TERM_TOUCH_EVENT_STATE_FLICK_DOWN) {
            _state = TERM_TOUCH_EVENT_STATE_FINISH_FLICK_DOWN;
        } else {
            _state = TERM_TOUCH_EVENT_STATE_RESUMED;
        }
    } else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
        //check direction
        if (_state != TERM_TOUCH_EVENT_STATE_FLICK_UP
            && _state != TERM_TOUCH_EVENT_STATE_FLICK_DOWN)
        {
            CGPoint translation = [((UIPanGestureRecognizer *)gestureRecognizer) translationInView:self];
            if (translation.y > TERM_LABEL_TRANSLATION_AMOUNT_TO_FLICK) {
                NSLog(@"+++DRAG DOWN+++");
                _state = TERM_TOUCH_EVENT_STATE_FLICK_DOWN;
            } else if (translation.y < -TERM_LABEL_TRANSLATION_AMOUNT_TO_FLICK) {
                NSLog(@"+++DRAG UP+++");
                _state = TERM_TOUCH_EVENT_STATE_FLICK_UP;
            }
        }
        else
        {
            //label move by touch point
            //assume that the superview is the root view where label can move around
            CGPoint touchPoint = [gestureRecognizer locationInView:self.superview];
            CGRect frame = self.frame;
//            NSLog(@"FRAME:%@ TOUCH:%@", NSStringFromCGRect(frame), NSStringFromCGPoint(touchPoint));
            frame.origin.x = touchPoint.x - (frame.size.width * 0.5f);
            frame.origin.y = touchPoint.y - (frame.size.height * 0.5f);
            self.frame = frame;
        }
    }
}

@end
